﻿#region Using Directives

using System;
using System.Text;
using EnvDTE;
using System.ComponentModel;
using Microsoft.VisualStudio.Shell;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.TeamArchitect.PowerTools.Features;

#endregion

namespace FeatureBuilder.ContribLib.ValueProviders
{
    /// <summary>
    /// Returns the current selected Project on the Solution Explorer
    /// Note: Only returns the project if it is selected itself!
    /// </summary>
    [DisplayName("CurrentProjectProvider")]
    [Description("Returns the selected Project on the Solution Explorer")]
    [Category("IDE")]
    public class CurrentProjectProvider : ValueProvider
    {
        /// <summary>
        /// Gets the ServiceProvider that we will use to get a reference to DTE.
        /// </summary>
        [Import(typeof(SVsServiceProvider))]
        public IServiceProvider ServiceProvider { get; private set; }

        #region Overrides

        /// <summary>
        /// Uses the DTE.SelectedItems collection to get the current selected project
        /// </summary>
        public override object Evaluate()
        {
            object selectedProject = null;
            DTE projectDTE = this.ServiceProvider.GetService(typeof(DTE)) as DTE;

            if (projectDTE != null)
            {
                if (projectDTE.SelectedItems.Count == 1)
                {
                    SelectedItem selection = projectDTE.SelectedItems.Item(1);
                    if (selection.Project != null)
                    {
                        selectedProject = selection.Project;
                    }
                }
            }
            return selectedProject;
        }

        #endregion
    }
}

